package com.lancoo.cvas.service.impl;

import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.lancoo.cvas.entity.TaskProcess;
import com.lancoo.cvas.mapper.TaskProcessMapper;
import com.lancoo.cvas.pojo.common.Deleted;
import com.lancoo.cvas.service.TaskProcessService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;


@Service("taskProcessService")
public class TaskProcessServiceImpl extends ServiceImpl<TaskProcessMapper, TaskProcess> implements TaskProcessService {
    @Resource
    private TaskProcessMapper taskProcessMapper;

    public static Map<String, Integer> coursePlanMap = new LinkedHashMap<>();
    public static Map<String, Integer> scheduleArrPlanMap = new LinkedHashMap<>();
    public static Map<String, Integer> courseOnlineMap = new LinkedHashMap<>();

    static {
        coursePlanMap.put("设置各校区上课学生", 1);
        coursePlanMap.put("设置专业/班级上课范围", 2);
        coursePlanMap.put("设置课程/环节参数", 3);
        coursePlanMap.put("拆班上课", 4);
        coursePlanMap.put("导入开课计划", 5);
        coursePlanMap.put("保存开课计划", 6);
        coursePlanMap.put("发布开课计划", 18);


        scheduleArrPlanMap.put("设置学期课时方案", 7);
        scheduleArrPlanMap.put("设置排课规则", 8);
        scheduleArrPlanMap.put("编排课表", 9);
        scheduleArrPlanMap.put("确认排课结果", 10);
        scheduleArrPlanMap.put("发布并同步至智校执行课表", 19);

        courseOnlineMap.put("设置选课轮次", 11);
        courseOnlineMap.put("设置正选学生选课时段", 12);
        courseOnlineMap.put("设置补选学生选课时段", 13);
        courseOnlineMap.put("设置退选学生选课时段", 14);
        courseOnlineMap.put("确认退选结果", 15);
        courseOnlineMap.put("确认选课结果", 16);
        courseOnlineMap.put("发布选课结果", 17);
        courseOnlineMap.put("发布选课结果应用至课表", 20);
    }


    @Override
    public void initProcess(Integer taskId, Integer mainFlow) {
        List<TaskProcess> list = new ArrayList<>();
        //开课计划
        coursePlanMap.forEach((key, value) -> {
            list.add(new TaskProcess().setTaskId(taskId).setProcessName(key).setPower(value).setIsFinish(0));
        });

        //课程编排
        scheduleArrPlanMap.forEach((key, value) -> {
            list.add(new TaskProcess().setTaskId(taskId).setProcessName(key).setPower(value).setIsFinish(0));
        });
        //在线选课
        courseOnlineMap.forEach((key, value) -> {
            list.add(new TaskProcess().setTaskId(taskId).setProcessName(key).setPower(value).setIsFinish(0));
        });
        taskProcessMapper.addBatch(list);
    }

    @Override
    public Boolean updateProcess(Integer taskId, int id, int finish) {
        LambdaUpdateWrapper<TaskProcess> updateWrapper = new LambdaUpdateWrapper<TaskProcess>().eq(TaskProcess::getTaskId, taskId)
                .eq(TaskProcess::getPower, id)
                .eq(TaskProcess::getDeleted, Deleted.NOT.getCode())
                .set(TaskProcess::getIsFinish, finish);
        boolean update = this.update(updateWrapper);
        return update;
    }

    @Override
    public List<TaskProcess> getProcessList(int taskId) {
        List<TaskProcess> processList = this.list(new LambdaQueryWrapper<TaskProcess>().eq(TaskProcess::getTaskId, taskId)
                .eq(TaskProcess::getDeleted, Deleted.NOT.getCode()).orderByAsc(TaskProcess::getPower));
        return processList;
    }

    @Override
    public TaskProcess getTaskProgressById(Integer taskId, Integer id) {

        //获取流程图对应状态
        List<TaskProcess> processList = this.list(new LambdaQueryWrapper<TaskProcess>().eq(TaskProcess::getTaskId, taskId)
                .eq(TaskProcess::getPower, id)
                .eq(TaskProcess::getDeleted, Deleted.NOT.getCode()));

        if (CollUtil.isNotEmpty(processList)) {
            return processList.get(0);
        }

        return new TaskProcess();
    }
}